package org.edu.controller;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;
import org.edu.bean.TmBizopen;
import org.edu.bean.TmCompinfo;
import org.edu.common.PageView;
import org.edu.common.QueryResult;
import org.edu.service.TmBizopenService;
import org.edu.service.TmCompinfoService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TmCompInfoController extends ActionSupport{
	@Resource
	private TmCompinfoService compinfoService;
	@Resource
	private TmBizopenService bizopenService;
	
	private TmCompinfo entity;
	private TmBizopen bizEntity;
	private Integer page = 1;
	private String taxplayer;
	private String message;
	
	@JSON(name="bizEntity")
	public TmBizopen getBizEntity() {
		return bizEntity;
	}

	public void setBizEntity(TmBizopen bizEntity) {
		this.bizEntity = bizEntity;
	}

	public String getTaxplayer() {
		return taxplayer;
	}

	public void setTaxplayer(String taxplayer) {
		this.taxplayer = taxplayer;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public TmCompinfo getEntity() {
		return entity;
	}

	public void setEntity(TmCompinfo entity) {
		this.entity = entity;
	}

	@Override
	public String execute() throws Exception {
		try {
			compinfoService.save(entity);
			this.addActionMessage("添加纳税企业基本信息成功");
		} catch (Exception e) {
			this.addActionMessage("添加纳税企业基本信息失败");
			e.printStackTrace();
		}
		return "message";
	}
	public String list() throws Exception {
		PageView<TmCompinfo> pageView = new PageView<TmCompinfo>(20,page);
		QueryResult<TmCompinfo> result = compinfoService.getQueryResult(pageView.getFirstResult(),pageView.getMaxresult());
		pageView.setQueryResult(result);
		ServletActionContext.getRequest().setAttribute("pageView",pageView);
		return SUCCESS;
	}
	public String check() throws Exception {
		this.bizEntity = bizopenService.findByTaxplayer(taxplayer);
		TmCompinfo compinfo = compinfoService.findByTaxplayer(taxplayer);
		if(compinfo != null){
			this.message = "基本信息中该企业已经存在";
			return "check2";
		}else{
			if(this.bizEntity != null){
				return "check";
			}else{
				this.message = "该企业纳税人不存在，请核定后输入";
				return "check2";
			}
		}
	}
}
